home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: Got Questions About Multi-Dimension Arrays (After Reading FAQ)
- Date: 01 Feb 1996 16:29:11 GMT
- Organization: Los Alamos National Laboratory
- Distribution: world
- Message-ID: <TANMOY.96Feb1092911@qcd.lanl.gov>
- References: <4ep87b$o60@alcor.usc.edu>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: wawda@alcor.usc.edu's message of 31 Jan 1996 18:23:39 -0800
-
- In article <4ep87b$o60@alcor.usc.edu> wawda@alcor.usc.edu (Abu Wawda)
- writes:
- <snip>
- I read questions 6.19 and 6.20 from the FAQ that deal with
- multi-dimensional arrays but am still confused. If someone can clear
- up my confusion, I would really appreciate it. Bascailly I still don't
- see why it isn't possible to do this:
-
- void myfunc(int *array,int rows,int cols)
- {
- printf("%d\n",array[1][1]);
- }
-
- You cannot do it because an int cannot be subscripted with another
- int. Consider that after int *array, array[1] is an int. So, what do
- you expect array[1][1] to be? What you have written is similar to
- writing int x; and then writing x[1] ... if you think about it, you
- will see that no natural interpretation is possible for this.
-
- I think your basic confusion is that you are thinking of
- `multi-dimensional' arrays. It might help you if you realized that the
- concept you have in your mind does not exist in C. C does have arrays
- of arrays (and other concepts like arrays of pointers and pointers to
- pointers etc.) which most people think of the same way as
- multi-dimensional arrays: but in understanding the nature of C
- operators, it is best to think of them as such. Thus array[1][2] (I am
- using [1][2] instead of [1][1] to avoid trivial confusion) is not the
- elementary operation take the 1,2 th element of array, but rather the
- operation of taking the second element from the first element of
- array. So, if the 1st element of array is not an array or a pointer to
- the first element of an array, taking its second element is a bit
- difficult :-)
-
- From your later question, it is clear that you do not mean int**
- instead of int* above: I bring that up becuase after int**, you can
- indeed write array[1][2] etc. But, remember that in this case, array
- is not an array of arrays of ints , but rather a pointer to the first
- element of an array of pointers to the first elements of arrays of
- ints. Note that that an array of arrays of ints means that the arrays
- of ints are packed closely together into one array and that is all
- there is to it; an array of pointers to the first elements of arrays
- of ints means that the individual arrays of ints are possibly
- scatterred all over memory and pointers to the first elements are
- stored closely packed into one array.
-
- and call the function like this:
-
- int m[2][2] = {1, 2, 3, 4};
-
- myfunc(m,2,2);
-
- Because when one says that an array decays to a pointer, one does not
- mean that a multidimensional array decyas to a pointer. As I said,
- conceptually there is nothing called a multidimensional array: and an
- array or arrays decays to a pointer to an array, not a pointer to an
- int.
-
- If I understand correctly, a multideminsional array is really stored
-
- Let is call it array of arrays.
-
- continguously like this in memory:
-
- Sure: an array of arrays is a contiguous sequence of arrays, each of
- which is a contiguous sequence of ints
-
- array[0][0] | 1 | memory address a
- array[0][1] | 2 | memory address a+sizeof(int)
- array[1][0] | 3 | ....
- array[1][1] | 4 | ....
-
- So why can't C convert from pointer to integer to a multidemsional
- array. The reason I want to do this is because my function needs to
-
- Because there is _no_ multidimensional array in C. Sorry.
-
- If you wanted a fixed dimension, then you could convert from an int *
- to a pointer to the first of a bunch of arrays. Thus
-
- void myfunc(int *array,int rows,int cols)
- {
- printf("%d\n",((int(*)[2])array)[1][1]);
- }
-
- followed by
-
- int m[2][2] = {1, 2, 3, 4};
-
- myfunc((int*)m,2,2);
-
- is indeed valid. The problem is that a _type_ in C cannot contain
- variables (and that rule goes very deep in C: a type is a compile time
- constant), and so you cannot have arrays of truly unknown dimension
- (that would be a variable type). So, the analogous expression
- (int(*)[cols])array is not C.
-
- take in an array of any width and height. Thank you for your time,
-
- The FAQ discusses the pros and cons of many of the methods there. If
- my answer is still not clear, please post further followups.
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-